package runesim.node.packethandler;
import org.apache.mina.common.IoSession;
import runesim.node.model.Player;
import runesim.node.net.*;
import runesim.node.ScriptMap;
import bsh.*;
public class WorldObjectHandler implements PacketHandler {
	
	@Override
	public void handlePacket(Packet p, IoSession session) {
		Player player = (Player) session.getAttachment();
		ActionSender sender = player.getActionSender();
		int  x = -1;
		int  y = -1;
		int id = -1;
		int type = -1;
		
		switch(p.getId()){
			case 132: //First click object
				x = p.readSignedWordBigEndian();
				id = p.readUnsignedWord();
				y = p.readUnsignedWordA();
				type = 1;
				break;
				
			case 252: //Second click object
				id = p.readUnsignedWordBigEndianA();
				y = p.readSignedWordBigEndian();
				x = p.readUnsignedWord();
				type = 2;
				break;
				
			case 70: //Third click object
				x = p.readSignedWordBigEndian();
				y = p.readUnsignedWord();
				id = p.readUnsignedWordBigEndianA();
				type = 3;
				break;
		}
		
		try {
			Interpreter i = ScriptMap.getMap().get(p.getId());
			Object[] params = new Object[]{x, y, id, type, player, sender};
			i.getNameSpace().invokeMethod("packetHandle", params, i);
		} catch(Exception e){
			System.out.println("Packet script error: "+p.getId());
		}
	}
}